package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.fengba.nanzhi.fengba.MainActivity;
import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.util.concurrent.CountDownLatch;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.fengba.nanzhi.utils.Function.getStatusBarHeight;

public class AddressActivity extends AppCompatActivity {
    private static final String TAG = "";
    private ImageButton mBtnshdzfh;
    private Button mBtnxzdz;
    private RecyclerView mRxhdzlb;
    private JSONArray jsonArray1;
    private TextView addressTop;
    private String user_id;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String xianshi = getIntent().getStringExtra("xianshi");
        if(xianshi!=null) {
            if (xianshi.equals("0")) {
                Toast.makeText(AddressActivity.this, "新增成功", Toast.LENGTH_SHORT).show();
            } else if (xianshi.equals("1")) {
                Toast.makeText(AddressActivity.this, "修改成功", Toast.LENGTH_SHORT).show();
            }
        }
        setContentView(R.layout.wz_activity_address);
        //加载数据
        init();
        //关闭页面
        mBtnshdzfh = findViewById(R.id.shdzfh);
        mBtnshdzfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }

        });

        //新增地址
        mBtnxzdz = findViewById(R.id.btnxzdz);
        mBtnxzdz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
                Intent intent = new Intent(AddressActivity.this,BjAdderssActivity.class);
                startActivity(intent);
            }
        });

        final RefreshLayout refreshLayout = (RefreshLayout)findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {

            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init();
                        if(jsonArray1==null) {
                            Toast.makeText(AddressActivity.this, "已经没有更多数据了", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        }else{
                            Toast.makeText(AddressActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                            refreshLayout.finishRefresh();
                        }
                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });
    }

    public void init() {
        Object user_info = Preferences.getParam(AddressActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());

            //账号
            user_id = myJson.get("id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type1019(user_id, new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                try {
                        jsonArray1 = (JSONArray) jsonObject.get("data");
                        mRxhdzlb = findViewById(R.id.shdzlb);
                        mRxhdzlb.setLayoutManager(new LinearLayoutManager(AddressActivity.this));
                        mRxhdzlb.setAdapter(new ShdzAdapter(AddressActivity.this,AddressActivity.this,jsonArray1));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {
                showToast(AddressActivity.this,"网络错误");
            }
        });
    }
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }

    public void setJsonArray(JSONArray jsonArray1) {
        this.jsonArray1 = jsonArray1;
    }

    public void top(){
        ViewGroup.LayoutParams para = addressTop.getLayoutParams();
        int top = getStatusBarHeight(AddressActivity.this);
        para.height = top;
        addressTop.setLayoutParams(para);
    }
}
